<!DOCTYPE html>
<html>
    <head>
        <title>Test MediaKeySession remove() function on temporary sessions</title>
        <script src="encrypted-media-utils.js"></script>
        <script src="../../resources/testharness.js"></script>
        <script src="../../resources/testharnessreport.js"></script>
    </head>
    <body>
        <script>
            promise_test(function(test)
            {
                var keyId = stringToUint8Array('0123456789012345');
                var rawKey = new Uint8Array([0xeb, 0xdd, 0x62, 0xf1, 0x68, 0x14, 0xd2, 0x7b,
                                             0x68, 0xef, 0x12, 0x2a, 0xfc, 0xe4, 0xae, 0x3c]);

                var mediaKeySession;
                var waitForMessagePromise;

                return navigator.requestMediaKeySystemAccess('org.w3.clearkey', getSimpleConfigurationForInitDataType('keyids')).then(function(access) {
                    return access.createMediaKeys();
                }).then(function(mediaKeys) {
                    mediaKeySession = mediaKeys.createSession();
                    waitForMessagePromise = waitForSingleEvent(mediaKeySession, 'message', function(e, resolve, reject) {
                        resolve();
                    });
                    const request = stringToUint8Array(createKeyIDs(keyId));
                    return mediaKeySession.generateRequest('keyids', request);
                }).then(function() {
                    return waitForMessagePromise;
                }).then(function() {
                    const jwkSet = stringToUint8Array(createJWKSet(createJWK(keyId, rawKey)));
                    return mediaKeySession.update(jwkSet);
                }).then(function() {
                    // After update() the session should have 1 usable key.
                    verifyKeyStatuses(mediaKeySession.keyStatuses,
                        [ { keyId: keyId, status: 'usable' } ]);
                    return mediaKeySession.remove();
                }).then(function() {
                    // After remove() all keys should be 'released'.
                    verifyKeyStatuses(mediaKeySession.keyStatuses,
                        [ { keyId: keyId, status: 'released' } ]);
                    // After remove() the session expiry should be NaN.
                    // ClearKey doesn't change set expiry times, but check
                    // anyway.
                    assert_true(isNaN(mediaKeySession.expiration));
                    return mediaKeySession.close();
                }).then(function() {
                    // After close() there should be no keys.
                    verifyKeyStatuses(mediaKeySession.keyStatuses, []);
                });

            }, 'Test MediaKeySession remove() function on temporary sessions');
        </script>
    </body>
</html>
